﻿using System;
using System.Collections.Generic;
using System.ComponentModel;

namespace Matrix.Common.Utility
{
    /// <summary>
    /// 时间帮助类
    /// </summary>
    public static class TimeHelper
    {
        /// <summary>
        /// 根据时间间隔类型获取时间段
        /// </summary>
        /// <param name="timeSpanType">时间间隔类型</param>
        /// <param name="timeSpanValue">间隔值</param>
        /// <returns>返回开始时间及结束时间（0位开始时间  1为结束时间）</returns>
        public static List<DateTime> GetTimeRangeByTimeSpanType(TimeSpanType timeSpanType, int timeSpanValue)
        {
            var result = new List<DateTime>();
            var now = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59"));
            timeSpanValue--;
            switch (timeSpanType)
            {
                case TimeSpanType.Day:
                    result.Add(DateTime.Parse(now.AddDays(0 - timeSpanValue).ToShortDateString()));
                    result.Add(now);
                    break;
                case TimeSpanType.Week:
                    var week = (int)now.DayOfWeek;
                    if (week == 0)
                    {
                        week = 7;
                    }
                    result.Add(DateTime.Parse(now.AddDays(0 - (week - 1) - 7 * timeSpanValue).ToShortDateString()));
                    result.Add(now.AddDays(7 - week));
                    break;
                case TimeSpanType.Month: 
                    result.Add(DateTime.Parse(now.AddMonths(0 - timeSpanValue).ToString("yyyy-MM-01")));
                    var days = DateTime.DaysInMonth(now.Year, now.Month);
                    result.Add(DateTime.Parse(now.ToString("yyyy-MM-" + days + " 23:59:59")));
                    break;
                case TimeSpanType.Year:
                    result.Add(DateTime.Parse(now.AddYears(0 - timeSpanValue).ToString("yyyy-01-01")));
                    result.Add(DateTime.Parse(now.ToString("yyyy-12-31 23:59:59")));
                    break;
            }
            return result;
        }
    }

    /// <summary>
    /// 时间间隔类型
    /// </summary>
    public enum TimeSpanType
    {
        [Description("自然日")]
        Day = 0,
        [Description("自然周")]
        Week = 1,
        [Description("自然月")]
        Month = 2,
        [Description("自然年")]
        Year = 3
    }
}
